﻿  @{ var guid = Guid.NewGuid(); }
<script language="javascript" type="text/javascript">
    $(function () {
        $(document).ready(function () {
            $('#form-@guid').validate();
        });
        var form = $('#form-@guid').ajaxForm({
            dataType: 'json',
            type: 'post',
            beforeSerialize: function ($form, options) {
                //inform the server you need JsonResult return
                var jsonResultHidden = form.find('input:hidden[name=JsonResult]').val(true);
                if (!jsonResultHidden.length) {
                    jsonResultHidden = $('<input type="hidden" name="JsonResult" value="true"/>').appendTo(form);
                }
            },
            success: function (response, textStatus) {
                if (response.Success) {
                    $.ajax({
                        url: "@SubmissionHelper.EmailUrl()",
                        context: document.body,
                        success: function () {
                        }
                    });
                    if (response.RedirectUrl) {
                        window.location.href = response.RedirectUrl;
                    } else {
                        document.getElementById('form-@guid').reset();
                        $('#successMessage').html("İletişim Formu Başarıyla Kaydedildi");
                    }
                } else {
                    $('#successMessage').html("");

                    var msgStr = '';
                    for (var i = 0; i < response.Messages.length; i++) {
                        msgStr += response.Messages[i] + '\r\n';
                    }

                    var validator = form.validate();
                    var obj = {};
                    var errLen = response.FieldErrors.length;
                    var fldMsg, fldErr, currMsg;
                    for (var i = 0; i < errLen; i++) {
                        fldErr = response.FieldErrors[i];
                        fldMsg = fldErr.ErrorMessage;

                        currMsg = obj[fldErr.FieldName];
                        obj[fldErr.FieldName] = currMsg ? (fldMsg ? currMsg + " " + fldMsg : currMsg) : fldMsg;
                    }
                    if (errLen > 0) {
                        validator.showErrors(obj);
                    }
                    if (msgStr) {
                        alert(msgStr);
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) { }
        });
    });
</script>
<div class="common-form">
  <br />
    <h5 class="title">İletişim Formu</h5>
  <div id="successMessage" class="message success close"></div>
    <form id="form-@guid"  method="post" action="@SubmissionHelper.CreateContentUrl()">
        @Html.AntiForgeryToken()
        <input type="hidden" name="FolderName" value="Contact" />
        <input type="hidden" name="Published" value="true" />
        <input type="hidden" name="JsonResult" value="true"/>
      <table style="height:300px; width:300px">
            <tbody>
                <tr>
                  <td style="width:80px"><label for="Name">Adınız:</label></td>
                    <td>
                      <input type="text" class="required" style="float:left" id="Name" name="Name" data-val="true" />
                        @Html.ValidationMessageForInput("Name")
                    </td>
                </tr>
                <tr>
                    <td><label for="EMail">E-Mailiniz:</label></td>
                    <td>
                        <input id="EMail" class="required email" name="EMail" data-val="true"/>
                      	@Html.ValidationMessageForInput("EMail")
                    </td>
                </tr>
          	 <tr>
                    <td><label for="Address">Adresiniz:</label></td>
                    <td>
                        <input id="Address" class="required" name="Address" data-val="true"/>
                      	@Html.ValidationMessageForInput("Address")
                    </td>
                </tr>
      		 <tr>
                    <td><label for="PhoneNo">Telefonunuz:</label></td>
                    <td>
                        <input id="PhoneNo" name="PhoneNo" data-val="true"/>
                        @Html.ValidationMessageForInput("PhoneNo")
                    </td>
                </tr>
  		 <tr>
                    <td><label for="Message">Mesajınız:</label></td>
                    <td>
                      <textarea class="required" style="border-width:1px; border-color:silver; border-style:solid" id="Message" name="Message" cols="25" rows="10" data-val="true"></textarea>
                      	@Html.ValidationMessageForInput("Message")
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="submit" value="Gönder" /></td>
                </tr>
            </tbody>
        </table>
    </form>
</div>
